<template>
  <DFModal
    ref="dfModalRef"
    :title="$t('common.table.chooseMachine')"
    @register="modalRegister"
    @on-ok="okModalFn"
  >
    <DFForm :route-name="routeName" get-label-form="table" @register="modalFormRegister" />
  </DFModal>
</template>

<script lang="ts" setup>
  import { DFModal, useModal } from '@render/components/DFModal';
  import { DFForm, useForm } from '@/components/DFForm/index';
  import { t } from '@/i18n';
  // import { useWsStore } from '@/store/modules/ws';

  // const wsStore = useWsStore();

  interface Props {
    machineList: any;
    machine: any;
    routeName?:string;
  }

  const props = withDefaults(defineProps<Props>(), {
    machineList: () => [],
    machine:()=>null,
    routeName:()=>'productionCenter'
  });

  const emit = defineEmits(['setChooseMachineCodeFn', 'register']);

  const [modalRegister, { openModal, closeModal: closeModalFn, setSubLoading: setSubLoadingFn }] =
    useModal({}); // dialog配置方法

  const [
    modalFormRegister,
    { validate: validateFn, getFieldsValue: getModalFormValFn, setFieldsValue },
  ] = useForm({
    gridProps: { cols: 1 as any },
    layout: 'horizontal',
    showActionButtonGroup: false,
    schemas: [
      {
        field: 'machineCode',
        component: 'NSelect',
        label: 'machineName',
        defaultValue: null,
        componentProps: {
          clearable: true,
          options: computed(() =>
            props.machineList.map((item) => ({
              label: item.machineName,
              value: item.machineCode,
              // disabled: !wsStore.onlineMachineCodeList.includes(item.machineCode),
            }))
          ),
        },
        rules: [{ required: true, message: t('common.formTipMsg.input'), trigger: ['change'] }],
      },
    ],
  }); // dialog表单配置方法

  async function okModalFn() {
    try {
      const isSubmit = await validateFn();

      if (!isSubmit) {
        const val = await getModalFormValFn();
        // modalSubmitFn();
        emit('setChooseMachineCodeFn', val, closeModalFn);
      }
    } catch (e) {
      console.log(e);

      setSubLoadingFn(false);
    }
  }

  function openModalFn() {
    // console.log(shiftList.value)

    openModal();

    nextTick(() => {
      setFieldsValue({
        machineCode: props.machine?.machineCode || null,
      });
    });
  }

  defineExpose({
    openModalFn,
  });
</script>

<style lang="less" scoped></style>
